<script>
import { deepClone } from "@/utils/index";
import { updateDefaultVal } from '../util.js'
import {
  getDataItemDetail,
  getDataBySQL,
} from "@/api/databasemanage/datasource.js";
export default {
  props: {
    fieldsIndex: {
      type: Number,
      required: true,
    },
    formConf: {
      type: Object,
      required: true,
    },
    curFormId: {
      type: Number
    },
    tabChildren: {
      type: Array
    },
    // 1 新增 2 查看 3 编辑
    isEdit: {
      type: Number
    }
  },
  data() {
    return {
      value: "",
      slot: [],
      confFields: []
    };
  },
  watch: {
    value(newValue) {
      this.confFields[this.fieldsIndex].__config__.defaultValue = newValue;
    },
  },
  async created() {
    if(this.tabChildren && this.tabChildren.length){
      this.confFields = this.tabChildren;
    }else{
       this.confFields = this.formConf.fields;
    }
    const conf = this.confFields[this.fieldsIndex];
    if (conf.__config__.dataType == "dataItem") {
      let dataItem = conf.__config__.dataItem;
      let res = await getDataItemDetail(dataItem);
      if (res.data.success) {
        if (res.data.data.length > 0) {
          conf.__slot__.options = res.data.data.map((ele) => {
            return { ...ele, label: ele.F_ItemName, value: ele.F_ItemValue };
          });
        }
      }
    }
    if (conf.__config__.dataType == "dataSource") {
      let dataSource = conf.__config__.dataSource;
      let showField = conf.__config__.showField;
      let saveField = conf.__config__.saveField;
      let res = await getDataBySQL(dataSource);
      if (res.data.success) {
        if (res.data.data.length > 0) {
          conf.__slot__.options = res.data.data.map((ele) => {
            return { ...ele, label: ele[showField], value: ele[saveField] };
          });
        }
      }
    }
    if (
      this.confFields[this.fieldsIndex]?.__config__?.optionType === "button" &&
      this.confFields[this.fieldsIndex]?.__slot__?.options
    ) {
      this.confFields[this.fieldsIndex].__slot__.options.forEach(
        (item) => {
          this.slot.push(
            <el-radio-button label={item.value} value={item.value}>
              {item.label}
            </el-radio-button>
          );
        }
      );
    } else {
      if(!this.confFields[this.fieldsIndex]?.__slot__?.options){
        return ;
      }
      this.confFields[this.fieldsIndex].__slot__.options.forEach(
        (item) => {
          this.slot.push(
            <el-radio label={item.value} value={item.value}>
              {item.label}
            </el-radio>
          );
        }
      );
    }
   if(this.confFields[this.fieldsIndex].__config__.defaultValue){
     this.value = this.confFields[this.fieldsIndex].__config__.defaultValue
   }
    //是否为查看
    if(this.isEdit==2){
      this.confFields[this.fieldsIndex].disabled = true;
    }
  },
  mounted(){
    this.$event.$on('updateFormConf',obj=>{
      if(obj?.fields){
        this.value = updateDefaultVal(this.confFields[this.fieldsIndex].__config__.formId,obj.fields);
      } 
    });
  },
  render: function (h) {
    return (
      <el-radio-group
        v-model={this.value}
        disabled={this.confFields[this.fieldsIndex].disabled}
        size={this.confFields[this.fieldsIndex].size}
        style={this.confFields[this.fieldsIndex].style}
      >
        {this.slot}
      </el-radio-group>
    );
  },
};
</script>
